# Xenomorph Hostline Evolution - scripted effects
# Put this file in: common/scripted_effects/

# CALL THIS FROM YOUR NECROPHAGE / ASSIMILATION / HOST-CONVERSION EVENT
# while ROOT/THIS is the host pop BEFORE it is converted into a Xenomorph.
# Example inside your conversion event, before kill/create/modify species:
#
# owner = { save_event_target_as = xeno_assimilating_country }
# xeno_record_hostline_assimilation = yes
#
# If your conversion event is country scoped, scope into the specific host pop first, then call this effect.

xeno_record_hostline_assimilation = {
	if = { limit = { xeno_host_is_reptilian = yes } owner = { change_variable = { which = xeno_reptilian_hosts_consumed value = 1 } } }
	if = { limit = { xeno_host_is_avian = yes } owner = { change_variable = { which = xeno_avian_hosts_consumed value = 1 } } }
	if = { limit = { xeno_host_is_mammalian = yes } owner = { change_variable = { which = xeno_mammalian_hosts_consumed value = 1 } } }
	if = { limit = { xeno_host_is_arthropoid = yes } owner = { change_variable = { which = xeno_arthropoid_hosts_consumed value = 1 } } }
	if = { limit = { xeno_host_is_molluscoid = yes } owner = { change_variable = { which = xeno_molluscoid_hosts_consumed value = 1 } } }
	if = { limit = { xeno_host_is_lithoid = yes } owner = { change_variable = { which = xeno_lithoid_hosts_consumed value = 1 } } }
	if = { limit = { xeno_host_is_aquatic = yes } owner = { change_variable = { which = xeno_aquatic_hosts_consumed value = 1 } } }
	if = { limit = { xeno_host_is_necroid = yes } owner = { change_variable = { which = xeno_necroid_hosts_consumed value = 1 } } }
	if = { limit = { xeno_host_is_psionic = yes } owner = { change_variable = { which = xeno_psionic_hosts_consumed value = 1 } } }
	if = { limit = { xeno_host_is_cybernetic = yes } owner = { change_variable = { which = xeno_cybernetic_hosts_consumed value = 1 } } }
}

# Use this from later events/techs/traditions if you want the unity perk to unlock two more stages.
xeno_unlock_apex_hostline_cultivation = {
	set_country_flag = xeno_apex_hostline_cultivation_unlocked
	add_modifier = { modifier = xeno_hostline_apex_cultivation days = -1 }
}


# Called from conversion events. Sets event_target:xeno_conversion_target_species
# to the appropriate species: hybrid if the host class has a hybrid unlocked
# (and event_target:xeno_host_pop_for_class points to a pop of that class),
# else the empire's main Xenomorph species.
#
# Expected scope: planet. Requires event_target:xeno_host_pop_for_class to be set
# to the pop being converted, and owner = (the converting Xeno country).
xeno_pick_conversion_species = {
	owner = { save_event_target_as = xeno_converting_country }

	# Default fallback: empire's main Xenomorph species
	event_target:xeno_converting_country = {
		owner_main_species = { save_event_target_as = xeno_conversion_target_species }
	}

	# If host class is reptilian and hybrid unlocked, override.
	if = {
		limit = {
			event_target:xeno_host_pop_for_class = { xeno_host_is_reptilian = yes }
			event_target:xeno_converting_country = { has_country_flag = xeno_reptilian_hybrid_species_created }
		}
		event_target:xeno_converting_country = {
			any_owned_species = {
				limit = { has_trait = trait_xeno_reptilian_host }
				save_event_target_as = xeno_conversion_target_species
			}
		}
	}
	if = {
		limit = {
			event_target:xeno_host_pop_for_class = { xeno_host_is_avian = yes }
			event_target:xeno_converting_country = { has_country_flag = xeno_avian_hybrid_species_created }
		}
		event_target:xeno_converting_country = {
			any_owned_species = {
				limit = { has_trait = trait_xeno_arboreal_host }
				save_event_target_as = xeno_conversion_target_species
			}
		}
	}
	if = {
		limit = {
			event_target:xeno_host_pop_for_class = { xeno_host_is_mammalian = yes }
			event_target:xeno_converting_country = { has_country_flag = xeno_mammalian_hybrid_species_created }
		}
		event_target:xeno_converting_country = {
			any_owned_species = {
				limit = { has_trait = trait_xeno_mammalian_host }
				save_event_target_as = xeno_conversion_target_species
			}
		}
	}
	if = {
		limit = {
			event_target:xeno_host_pop_for_class = { xeno_host_is_arthropoid = yes }
			event_target:xeno_converting_country = { has_country_flag = xeno_arthropoid_hybrid_species_created }
		}
		event_target:xeno_converting_country = {
			any_owned_species = {
				limit = { has_trait = trait_xeno_acid_gland }
				save_event_target_as = xeno_conversion_target_species
			}
		}
	}
	if = {
		limit = {
			event_target:xeno_host_pop_for_class = { xeno_host_is_molluscoid = yes }
			event_target:xeno_converting_country = { has_country_flag = xeno_molluscoid_hybrid_species_created }
		}
		event_target:xeno_converting_country = {
			any_owned_species = {
				limit = { has_trait = trait_xeno_aquatic_host }
				save_event_target_as = xeno_conversion_target_species
			}
		}
	}
	if = {
		limit = {
			event_target:xeno_host_pop_for_class = { xeno_host_is_lithoid = yes }
			event_target:xeno_converting_country = { has_country_flag = xeno_lithoid_hybrid_species_created }
		}
		event_target:xeno_converting_country = {
			any_owned_species = {
				limit = { has_trait = trait_xeno_lithoid_host }
				save_event_target_as = xeno_conversion_target_species
			}
		}
	}
	if = {
		limit = {
			event_target:xeno_host_pop_for_class = { xeno_host_is_aquatic = yes }
			event_target:xeno_converting_country = { has_country_flag = xeno_aquatic_hybrid_species_created }
		}
		event_target:xeno_converting_country = {
			any_owned_species = {
				limit = { has_trait = trait_xeno_aquatic_host }
				save_event_target_as = xeno_conversion_target_species
			}
		}
	}
	if = {
		limit = {
			event_target:xeno_host_pop_for_class = { xeno_host_is_necroid = yes }
			event_target:xeno_converting_country = { has_country_flag = xeno_necroid_hybrid_species_created }
		}
		event_target:xeno_converting_country = {
			any_owned_species = {
				limit = { has_trait = trait_xeno_spore_host }
				save_event_target_as = xeno_conversion_target_species
			}
		}
	}
	if = {
		limit = {
			event_target:xeno_host_pop_for_class = { xeno_host_is_psionic = yes }
			event_target:xeno_converting_country = { has_country_flag = xeno_psionic_hybrid_species_created }
		}
		event_target:xeno_converting_country = {
			any_owned_species = {
				limit = { has_trait = trait_xeno_adrenal }
				save_event_target_as = xeno_conversion_target_species
			}
		}
	}
	if = {
		limit = {
			event_target:xeno_host_pop_for_class = { xeno_host_is_cybernetic = yes }
			event_target:xeno_converting_country = { has_country_flag = xeno_cybernetic_hybrid_species_created }
		}
		event_target:xeno_converting_country = {
			any_owned_species = {
				limit = { has_trait = trait_xeno_cybernetic_host }
				save_event_target_as = xeno_conversion_target_species
			}
		}
	}
}


# Creates a hybrid Xenomorph species using:
#   - event_target:xeno_hybrid_host_template - the original host species to template from
#   - event_target:xeno_hybrid_class_trait   - the xeno_subspecies trait to apply
# Sets has_country_flag = xeno_<class>_hybrid_species_created after creation.
# Caller is responsible for setting the per-class hybrid_species_created flag.
#
# Expected scope: country.
xeno_create_hybrid_species_template = {
	create_species = {
		name = event_target:xeno_hybrid_host_template
		namelist = event_target:xeno_hybrid_host_template
		portrait = event_target:xeno_hybrid_host_template
		plural = event_target:xeno_hybrid_host_template
		class = event_target:xeno_hybrid_host_template
		homeworld = capital_scope
		traits = {
			trait = trait_xenomorph
			trait = event_target:xeno_hybrid_class_trait
			ingame_modding = yes
		}
		effect = {
			set_species_flag = xeno_hybrid_species
			save_event_target_as = xeno_hybrid_new_species
		}
	}
}
